<script lang="ts" name="person-new" setup>
import { ref,computed } from "vue";

let firstName = ref('zhang')
let lastName = ref('san')

//计算属性====这么定义的fullName 且是只读的
// let fullName =  computed( () => {
//   return firstName.value.slice(0,1).toUpperCase() + firstName.value.slice(1) + '-' + lastName.value
// })

//这么写是可读可写的
let fullName =  computed({
  get() {
    return firstName.value.slice(0, 1).toUpperCase() + firstName.value.slice(1) + '-' + lastName.value
  },
  set(val) {
    const [str1, str2] = val.split('-')
    firstName.value = str1
    lastName.value = str2
    console.log('set',val)
  }
})

function changeFullName() {
  // console.log(fullName) //ComputedRefImpl
  fullName.value = '李-四' //报错 因为fullName是只读的
}
</script>

<template>
  <div class="person">
    姓: <input type="text" v-model="firstName"> <br>
    名: <input type="text" v-model="lastName"> <br>
    <button @click="changeFullName">将全名改为李-四</button>
    全名: <span>{{ fullName }}</span><br>
    全名: <span>{{ fullName }}</span>
    全名: <span>{{ fullName }}</span>
  </div>
</template>

<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}
</style>